---
# Jekyll layout that compresses HTML
# v2.0.0
# http://jch.penibelst.de/
# © 2014–2015 Anatol Broder
# MIT License
---
{% capture compresscache %}
{% if site.data.compress.html == false or site.compress_html.ignore.envs contains jekyll.environment %}
{% capture result %}{{ content }}{% endcapture %}
{% else %}

<!-- Don not touch this: start -->
{% capture nl0 %}
{% endcapture %}

{% capture nl1 %} 
{% endcapture %}

{% capture nl2 %}

{% endcapture %}

{% capture nl3 %}
 {% endcapture %}

{% capture nl4 %}{{ nl0 | prepend:">" | append:"<" }}{% endcapture %}

{% capture nl5 %}{{ nl1 | prepend:">" | append:"<" }}{% endcapture %}

{% capture nl6 %}{{ nl2 | prepend:">" | append:"<" }}{% endcapture %}

{% capture nl7 %}{{ nl3 | prepend:">" | append:"<" }}{% endcapture %}

<!-- Do not touch this: end -->

  {% capture _content %}{{ content }}{% endcapture %}

  {% assign _profile = site.compress_html.profile %}
  {% if site.compress_html.endings == "all" %}
    {% assign _endings = "html head body dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}
  {% else %}
    {% assign _endings = site.compress_html.endings %}
  {% endif %}

  {% for _element in _endings %}
    {% capture _end %}</{{ _element }}>{% endcapture %}
    {% assign _content = _content | remove: _end %}
  {% endfor %}

  {% if _profile and _endings %}
    {% assign _profile_endings = _content | size | plus: 1 %}
  {% endif %}

  {% assign _pre_befores = _content | split: "<pre" %}
  {% assign _content = "" %}
  {% for _pre_before in _pre_befores %}
    {% assign _pres = _pre_before | split: "</pre>" %}
    {% case _pres.size %}
      {% when 2 %}
        {% capture _content %}{{ _content }}<pre{{ _pres.first }}</pre>{{ _pres.last | replace:'	',' ' | replace:'     ',' ' | replace:'    ',' ' | replace:'   ',' ' | replace:'  ',' ' | remove: nl1 | remove: nl2 | remove: nl3 | replace:nl4,"><" | replace:nl5,"><" | replace:nl6,"><" | replace:nl7,"><" }}{% endcapture %}
      {% when 1 %}
        {% capture _content %}{{ _content }}{{ _pres.last | replace:'	', ' ' | replace:'     ',' ' | replace:'    ',' ' | replace:'   ',' ' | replace:'  ',' ' | remove: nl1 | remove: nl2 | remove: nl3 | replace:nl4,"><" }}{% endcapture | replace:nl4,"><" | replace:nl5,"><" | replace:nl6,"><" | replace:nl7,"><" %}
    {% endcase %}
  {% endfor %}

  {% if _profile %}
    {% assign _profile_collapse = _content | size | plus: 1 %}
  {% endif %}

  {% if site.compress_html.comments == "all" %}
    {% assign _comments = "<!-- -->" | split: " " %}
  {% else %}
    {% assign _comments = site.compress_html.comments %}
  {% endif %}

  {% if _comments.size == 2 %}
    {% assign _comment_befores = _content | split: _comments.first %}
    {% for _comment_before in _comment_befores %}
      {% assign _comment_content = _comment_before | split: _comments.last | first %}
      {% if _comment_content %}
        {% capture _comment %}{{ _comments.first }}{{ _comment_content }}{{ _comments.last }}{% endcapture %}
        {% assign _content = _content | remove: _comment %}
      {% endif %}
    {% endfor %}

    {% if _profile %}
      {% assign _profile_comments = _content | size | plus: 1 %}
    {% endif %}
  {% endif %}

  {% if site.compress_html.clippings == "all" %}
    {% assign _clippings = "html head title base link meta style body article section nav aside h1 h2 h3 h4 h5 h6 hgroup header footer address p hr blockquote ol ul li dl dt dd figure figcaption main div table caption colgroup col tbody thead tfoot tr td th" | split: " " %}
  {% else %}
    {% assign _clippings = site.compress_html.clippings %}
  {% endif %}

  {% for _element in _clippings %}
    {% assign _edges = " <e;<e; </e>;</e>;</e> ;</e>" | replace: "e", _element | split: ";" %}
    {% assign _content = _content | replace: _edges[0], _edges[1] | replace: _edges[2], _edges[3] | replace: _edges[4], _edges[5] %}
  {% endfor %}

  {% if _profile and _clippings %}
    {% assign _profile_clippings = _content | size | plus: 1 %}
  {% endif %}

  {% capture result %}{{ _content }}{% if _profile %} <table id="compress_html_profile_{{ site.time | date: "%Y%m%d" }}" class="compress_html_profile"> <thead> <tr> <td>Step <td>Bytes <tbody> <tr> <td>raw <td>{{ content | size }}{% if _profile_endings %} <tr> <td>endings <td>{{ _profile_endings }}{% endif %}{% if _profile_collapse %} <tr> <td>collapse <td>{{ _profile_collapse }}{% endif %}{% if _profile_comments %} <tr> <td>comments <td>{{ _profile_comments }}{% endif %}{% if _profile_clippings %} <tr> <td>clippings <td>{{ _profile_clippings }}{% endif %} </table>{% endif %}{% endcapture %}

{% endif %}
{% endcapture %}{% assign compresscache = nil %}{{ result }}
